package cn.yeamin.ms.mapper;

import java.util.List;
import java.util.Set;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import cn.yeamin.ms.vo.Resource;
import tk.mybatis.mapper.common.Mapper;

/**
 * @packageName: 包名: cn.yeamin.ms.mapper
 * @classDesc: 类功能描述:()
 * @author: Administrator
 * @createTime: 2017/7/25 14:22
 * @version: v1.0
 * @copyright:奕明(上海)影视传媒有限公司
 */
public interface ResourceMapper extends Mapper<Resource> {

    @Select("SELECT tb_resource.* FROM tb_user INNER JOIN tb_user_role ON tb_user.id = tb_user_role.user_id INNER JOIN tb_role ON tb_user_role.role_id = tb_role.id INNER JOIN tb_role_resource ON tb_user_role.role_id = tb_role_resource.role_id INNER JOIN tb_resource ON tb_role_resource.resource_id = tb_resource.id WHERE tb_user.id = #{id} AND tb_role. STATUS = 1")
    Set<Resource> findResourceByUserId(Long id);

    //@Select("SELECT r1.* FROM tb_resource r1 LEFT OUTER JOIN tb_resource r2 ON r1.parent_id = r2.id ORDER BY r2.id ASC, r1.id ASC, r1.sort ASC")
	List<Resource> queryAllTree();

	@Select("SELECT re.* FROM tb_role ro INNER JOIN tb_role_resource rr ON ro.id = rr.role_id INNER JOIN tb_resource re ON re.id = rr.resource_id WHERE ro.id = #{roleId} AND ro.STATUS = 1")
	Set<Resource> queryByIdForResources(Long roleId);
	
	@Delete("delete from tb_role_resource where role_id = #{roleId} AND resource_id = #{rid}")
	void delResourceAndRole(@Param("roleId")Long roleId, @Param("rid")Long rid);
	
	@Insert("insert into tb_role_resource values(#{roleId},#{rid})")
	void grant(@Param("roleId")Long roleId, @Param("rid")Long rid);

	@Delete("delete from tb_role_resource where role_id = #{roleId}")
	void deleteAll(Long roleId);

}
